package com.wufeng.mycode;


import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;


/**
 * ArrayList
 * 1、底层就是创建了一个数组
 * 2、默认大小10
 * 3、当长度要大于原来大小时，扩容1.5倍
 * 4、线程不安全，多线程add方法 抛异常 ConcurrentModificationException
 * (1)用Vector（尽量不用）
 * (2)Collections.synchronizedList(new ArrayList<>())
 * (3)CopyOnWriteArrayList
 * <p>
 * Set -> Collections.synchronizedSet(new HsahSet<>()),CopyOnWriteArraySet
 * <p>
 * HashMap ->Collections.synchronizedMap(new HashMap<>()),ConcurrentHashMap
 * <p>
 * list 使用Iterator
 */
public class ArrayListDemo {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(4);
        list.add(1);
        list.add(2);
        list.add(4);
        list.add(3);
        list.remove((Integer) 4);
        System.out.println(Arrays.toString(list.toArray()));
        for (Integer i : list) {
            System.out.println(i);
        }

//        Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
//        deleteDemo();
//        List<String> list = new CopyOnWriteArrayList();
//        Iterator iterator = list.iterator();
//        for (int i = 0; i < 30; i++) {
//            new Thread(() -> {
//                list.add(UUID.randomUUID().toString().substring(0,8));
//                System.out.println(list);
//            },String.valueOf(i)).start();
//
//        }
//        try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e){e.printStackTrace();}
//        System.out.println("++++++++++");
//        list.forEach(System.out::print);
    }

    public static void deleteDemo() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.remove(1);
        System.out.println(Arrays.toString(list.toArray()));
    }
}
